package pannelli;

import it.jack.Main;

import java.awt.Dimension;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JButton;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

import database.DB;

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;


import oggetti.Cliente;
import oggetti.Riparazione;
import pannelli.main.PannelloClienti;
import pannelli.main.PannelloRicerca;
import pannelli.sub.SubPannelloCliente;
import pannelli.sub.SubPannelloVisualizzaClienti;
import pannelli.sub.SubPannelloVisualizzaRiparazione;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class VisualizzaCliente extends javax.swing.JFrame {

    private JButton SalvaModifiche;
    private JButton next;
    private JButton prec;
    private JButton AggiungiRiparazioni;
    private JButton CancellaCliente;
    private DefaultTableModel model;
    private JScrollPane jScrollPane1;
    private JButton salva;
    private Cliente cliente;
    private int actual;
    private int max;
    private static VisualizzaCliente instance;

    private class DispatcherKeyListener implements KeyEventDispatcher {

        @Override
        public boolean dispatchKeyEvent(KeyEvent e) {
            if (e.getID() == KeyEvent.KEY_PRESSED) {
                int key = e.getKeyCode();
                if (key == KeyEvent.VK_F9 && actual > 0) {
                    precPressed();
                } else if (key == KeyEvent.VK_F10 && actual < max - 1) {
                    nextPressed();
                }
            }
            return false;
        }
    }

    public static VisualizzaCliente getInstance() {
        return instance;
    }
    public void updateActualMax(int actual,int max){
       this.actual=actual;
       this.max=max;
    }

    public void setModifica() {
        salva.setEnabled(true);
    }

    public VisualizzaCliente(Cliente cliente, int actual, int max) {
        super();
        this.actual = actual;
        this.max = max;
        this.cliente = cliente;
        initGUI();
        addListener();
        SubPannelloCliente.getInstance().fillFields(cliente);
        disabilita();
        KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        manager.addKeyEventDispatcher(new DispatcherKeyListener());
        this.instance = this;
    }

    private void disabilita() {
        //actual = flag?PannelloClienti.getIstance().getCurrentRow():SubPannelloVisualizzaClienti.getInstance().;
        int row = actual + 1;
        //max = PannelloClienti.getIstance().getMaxRow();
        boolean s = true, p = true;

        if (max == 0 || max == 1) {
            s = false;
            p = false;
        } else if (row == 1) {
            p = false;
        } else if (row == max) {
            s = false;
        }
        next.setEnabled(s);
        prec.setEnabled(p);
    }

    public void updateTable(Riparazione r) {
        int a = model.getRowCount();
        for (int i = 0; i < a; i++) {
            model.removeRow(0);
        }

        model.addRow(r.toArray());
    }

    private void initGUI() {
        try {
            AnchorLayout thisLayout = new AnchorLayout();
            getContentPane().setLayout(thisLayout);
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            {
                salva = new JButton();
                getContentPane().add(salva, new AnchorConstraint(901, 575, 961, 399, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
                salva.setText("Salva Modifiche");
                salva.setPreferredSize(new java.awt.Dimension(125, 24));
                salva.setEnabled(false);
            }
            {
                next = new JButton();
                getContentPane().add(next, new AnchorConstraint(904, 975, 961, 825, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
                next.setText("Succ ( F10 ) >>");
                next.setPreferredSize(new java.awt.Dimension(106, 23));
            }
            {
                prec = new JButton();
                getContentPane().add(prec, new AnchorConstraint(899, 165, 961, 28, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
                prec.setText("<< Prec ( F9 )");
                prec.setPreferredSize(new java.awt.Dimension(97, 25));
            }
            {
                SubPannelloVisualizzaRiparazione.getInstance().init(cliente.getId());
                getContentPane().add(SubPannelloVisualizzaRiparazione.getInstance(), new AnchorConstraint(477, 983, 871, 0, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
                SubPannelloVisualizzaRiparazione.getInstance().setPreferredSize(new java.awt.Dimension(680, 140));
            }
            {
                AggiungiRiparazioni = new JButton();
                getContentPane().add(AggiungiRiparazioni, new AnchorConstraint(899, 804, 964, 582, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
                AggiungiRiparazioni.setText("Aggiungi Riparazioni");
                AggiungiRiparazioni.setPreferredSize(new java.awt.Dimension(157, 26));
            }
            {
                CancellaCliente = new JButton();
                getContentPane().add(CancellaCliente, new AnchorConstraint(899, 383, 961, 180, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
                CancellaCliente.setText("Cancella Cliente");
                CancellaCliente.setPreferredSize(new java.awt.Dimension(144, 25));
                CancellaCliente.setEnabled(Main.getInstance().getUser().getUsername().equals("admin"));
            }
            {
                getContentPane().add(SubPannelloCliente.getInstance(), new AnchorConstraint(1, 1000, 443, 0, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
                SubPannelloCliente.getInstance().setPreferredSize(new java.awt.Dimension(692, 157));
            }
            pack();
            setVisible(true);
            this.setSize(708, 425);
            centra();
            //subPannelloCliente1.riempiID();
            setResizable(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void centra() {
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation(new Point((dimension.width - getSize().width) / 2,
                (dimension.height - getSize().height) / 2));

    }

    private void precPressed() {
        if (actual == 1) {
            prec.setEnabled(false);
        }
        if (actual == max - 1) {
            next.setEnabled(true);
        }

        actual--;
        checkPanel();
    }
    private void checkPanel(){
       if(PannelloClienti.getIstance()!=null){
           Cliente c = PannelloClienti.getIstance().getClienteFromRow(actual);
          SubPannelloCliente.getInstance().fillFields(c);
          SubPannelloVisualizzaRiparazione.getInstance().updateTable(c.getId());
       }else{
           Cliente c = PannelloRicerca.getInstance().getClienteFromRow(actual);
          SubPannelloVisualizzaRiparazione.getInstance().updateTable(c.getId());
          SubPannelloCliente.getInstance().fillFields(c);
       }
       
    }
    private void nextPressed() {
        if (actual == max - 2) {
            next.setEnabled(false);
        }
        if (actual == 0) {
            prec.setEnabled(true);
        }

        actual++;
        checkPanel();
        //subPannelloCliente1.fillFields(PannelloClienti.getIstance().getClienteFromRow(actual));
    }
    public void updateTable(){
       SubPannelloVisualizzaRiparazione.getInstance().updateTable();
    }

    private void addListener() {
        CancellaCliente.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if (JOptionPane.showConfirmDialog(null, "Sei sicuro di voler cancellare questo cliente?", "Cancella", JOptionPane.YES_NO_OPTION) == 0) {
                    new DB().cancellaCliente(SubPannelloCliente.getInstance().getID());
                    if (max == 1) {
                        dispose();
                    } else if (actual == 0) {
                        nextPressed();
                    } else {
                        precPressed();
                    }
                    SubPannelloVisualizzaClienti.getInstance().initTable();
                }

            }
        });
        salva.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                new DB().salvaModificheCliente(SubPannelloCliente.getInstance().getClienteFromFields());
                PannelloClienti.getIstance().updateTable();
            }
        });
        AggiungiRiparazioni.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                new AggiungiRiparazione(cliente,1);
            }
        });
        prec.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                precPressed();
            }
        });
        next.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                nextPressed();
            }
        });
    }
}
